import time
import threading
from iClouds.libs import header_has_guuid
import random

zombie_data = [
    {
        "id": 1,
        "name": "小僵尸",
        "atk": 10,  # 攻击力
        "hp": 100,  # 生命值
        "range": 1,  # 攻击范围
        "rechar": 2,  # 攻击间隔
        "pt": 0,  # 植物准备时间
        "cd": 0,  # 道具冷却时间
        "cost": 0,  # 价格
        "dp": 50,  # 损伤点
        "crp": 50,  # 临界点
        "move": 10,  # 移动速度
        "jewelry": None,  # 道具
    }
]

plant_data = [
    {
        "id": 1,
        "name": "太阳花",
        "atk": 30,  # 攻击力
        "hp": 100,  # 生命值
        "range": 5,  # 攻击范围
        "rechar": 10,  # 攻击间隔
        "pt": 3,  # 植物准备时间
        "cd": 5,  # 道具冷却时间
        "cost": 10,  # 价格
        "dp": 60,  # 损伤点
        "crp": 30,  # 临界点
        "move": 30,  # 移动速度 子弹速度
        "jewelry": None,  # 道具
    },
    {
        "id": 2,
        "name": "豌豆射手",
        "atk": 10,  # 攻击力
        "hp": 100,  # 生命值
        "range": 11,  # 攻击范围
        "rechar": 3,  # 攻击间隔
        "pt": 3,  # 植物准备时间
        "cd": 5,  # 道具冷却时间
        "cost": 20,  # 价格
        "dp": 60,  # 损伤点
        "crp": 30,  # 临界点
        "move": 30,  # 移动速度 子弹速度
        "jewelry": None,  # 道具
    },
]

jewelry_data = [
    {
        "id": 1,
        "name": "路障",
        "atk": 0,  # 攻击力
        "hp": 50,  # 生命值
        "range": 0,  # 攻击范围
        "rechar": 0,  # 攻击间隔
        "pt": 0,  # 植物准备时间
        "cd": 0,  # 道具冷却时间
        "cost": 0,  # 价格
        "dp": 30,  # 损伤点
        "crp": 0,  # 临界点
        "move": 0,  # 移动速度
    }
]


"""
A:当植物生命值低于临界点时植物立刻消亡。

B:当僵尸生命值低于临界点时僵尸会失去所有的僵尸攻击功能（不再啃咬植物，
也无法触发土豆雷、食人花、窝瓜和水草等植物的攻击），生命值开始匀速减少，
在生命值减到0之前，僵尸的躯体保持原有运动状态和速度，并吸引和吸收火力。

游戏共有26种不同的僵尸敌人，包括开着车子的雪橇车僵尸和投石车僵尸等。49种功能互不相同

饰品指路障，铁桶，报纸，铁门，橄榄球头盔，雪橇车，南瓜头，矿工帽，铁梯，僵尸坚果，
僵尸大坚果这些可为本体提供额外防护的物品
"""


def has_zombie(index):
    """查询僵尸 根据ID"""
    for i in zombie_data:
        if i["id"] == index:
            return i
    else:
        return None


def has_plant(index):
    """查询植物 根据ID"""
    for i in plant_data:
        if i["id"] == index:
            return i
    else:
        return None


def has_jewelry(index):
    """查询道具 根据ID"""
    for i in jewelry_data:
        if i["id"] == index:
            return i
    else:
        return None


class baseUnity(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.isRun = True
        self.status = "fight"

        self.name = ""
        self.atk = 0  # 攻击力
        self.hp = 0  # 生命值
        self.range = 0  # 攻击范围
        self.rechar = 0  # 攻击间隔
        self.pt = 0  # 植物准备时间
        self.cd = 0  # 道具冷却时间
        self.cost = 0  # 价格
        self.dp = 0  # 损伤点
        self.crp = 0  # 临界点
        self.move = 0  # 移动速度

    def stop(self):
        self.isRun = False


class zombie(baseUnity):
    """僵尸"""

    def __init__(self, room):
        super().__init__()
        self.room = room
        self.guid = header_has_guuid()
        # ---------------其他特性----------------------
        self.fire = time.time()  # 上次攻击时间

    def setAttrs(self, data):
        # data = {
        #     "id": 1,
        #     "name": "小僵尸",
        #     "atk": 10,  # 攻击力
        #     "hp": 100,  # 生命值
        #     "range": 1,  # 攻击范围
        #     "rechar": 2,  # 攻击间隔
        #     "pt": 0,  # 植物准备时间
        #     "cd": 0,  # 道具冷却时间
        #     "cost": 0,  # 价格
        #     "dp": 50,  # 损伤点
        #     "crp": 50,  # 临界点
        #     "move": 10,  # 移动速度
        #     "jewelry": None,  # 道具
        # }
        self.data = data

    def setPoint(self, point):
        """初始化位置"""
        self.point = point  # 当前位置
        self.xy = [point[0] * self.room.min_gap[0], point[1] * self.room.min_gap[1]]

    def setTools(self, jewelry):
        """绑定道具"""
        self.data["jewelry"] = jewelry

    def __del__(self):
        try:
            self.room.zombie.remove(self)
        except:
            pass

    def run(self):
        print(f"初始化僵尸 {self.name}")
        self.room.alertAIring(
            {"act": "newZombie", "guid": self.guid, "data": self.data, "point": self.xy}
        )
        while self.isRun:
            time.sleep(2)
            if self.status == "death":
                break
            if self.xy[0] > 0:
                # 移动
                self.xy[0] -= self.data["move"]
                self.room.alertAIring(
                    {
                        "act": "move",
                        "guid": self.guid,
                        "data": self.data,
                        "point": self.xy,
                    }
                )
            # 攻击


class plant(baseUnity):
    """植物"""

    def __init__(self, room):
        super().__init__()
        self.room = room
        self.guid = header_has_guuid()
        # ---------其他特性--------------------
        self.last = time.time()

    def setAttrs(self, data):
        # data = {
        #     "id": 1,
        #     "name": "太阳花",
        #     "atk": 30,  # 攻击力
        #     "hp": 0,  # 生命值
        #     "range": 0,  # 攻击范围
        #     "rechar": 10,  # 攻击间隔
        #     "pt": 0,  # 植物准备时间
        #     "cd": 0,  # 道具冷却时间
        #     "cost": 10,  # 价格
        #     "dp": 6,  # 损伤点
        #     "crp": 0,  # 临界点
        #     "move": 0,  # 移动速度
        #     "jewelry": None,  # 道具
        # }
        self.data = data

    def setPoint(self, point):
        self.point = point  # 轨道位置 (0,3)
        self.xy = (point[0] * self.room.min_gap[0], point[1] * self.room.min_gap[1])

    def __del__(self):
        try:
            self.room.plant.remove(self)
        except:
            pass

    def run(self):
        print(f"初始化植物 {self.name}")
        self.room.alertAIring(
            {"act": "newPlant", "guid": self.guid, "data": self.data, "point": self.xy}
        )
        while self.isRun:
            time.sleep(1)
            if self.status == "death":
                break
            if (time.time() - self.last) > self.data["rechar"]:
                if self.data["id"] in [
                    1,
                ]:
                    self.room.newStarFromPlant(self.data)
                else:
                    self.room.newSlugFromPlant(self.data, self.point)
                self.last = time.time()


class shoping(baseUnity):
    """卡片"""

    def __init__(self, room):
        super().__init__()
        self.room = room
        self.guid = header_has_guuid()

    def setAttrs(self, data):
        # data = {
        #     "id": 1,
        #     "name": "太阳花",
        #     "atk": 0,  # 攻击力
        #     "hp": 0,  # 生命值
        #     "range": 0,  # 攻击范围
        #     "rechar": 10,  # 攻击间隔
        #     "pt": 0,  # 植物准备时间
        #     "cd": 0,  # 道具冷却时间
        #     "cost": 10,  # 价格
        #     "dp": 6,  # 损伤点
        #     "crp": 0,  # 临界点
        #     "move": 0,  # 移动速度
        #     "jewelry": None,  # 道具
        # }
        self.data = data

    def setPoint(self, point):
        self.point = point  # 位置块
        self.xy = point

    def __del__(self):
        try:
            self.room.card.remove(self)
        except:
            pass

    def run(self):
        print(f"初始化卡片 {self.name}")
        self.room.alertAIring(
            {"act": "newCard", "guid": self.guid, "data": self.data, "point": self.xy}
        )
        while self.isRun:
            time.sleep(1)


class moneyStar(baseUnity):
    """星星"""

    def __init__(self, room):
        super().__init__()
        self.room = room
        self.guid = header_has_guuid()

    def setAttrs(self, data):
        self.data = data

    def setPoint(self, point):
        self.point = point  # 位置块

    def __del__(self):
        try:
            self.room.sunlight.remove(self)
        except:
            pass

    def run(self):
        self.room.alertAIring(
            {
                "act": "newStar",
                "guid": self.guid,
                "data": self.data,
                "point": self.point,
            }
        )
        while self.isRun:
            time.sleep(1)


class fireSlug(baseUnity):
    """子弹"""

    def __init__(self, room):
        super().__init__()
        self.room = room
        self.guid = header_has_guuid()

    def setAttrs(self, data):
        self.data = data

    def setPoint(self, point):
        self.point = point  # 位置块
        self.xy = [
            point[0] * self.room.min_gap[0],
            point[1] * self.room.min_gap[1],
        ]  # 位置像素

    def __del__(self):
        try:
            self.room.slug.remove(self)
        except:
            pass

    def run(self):
        # print(f"初始化卡片 {self.name}")
        self.room.alertAIring(
            {
                "act": "fireSlug",
                "guid": self.guid,
                "data": self.data,
                "point": self.xy,
            }
        )
        while self.isRun:
            time.sleep(0.1)
            self.xy[0] += self.data["move"]
            if self.xy[0] > 900:
                break
